//
// Created by wangyg98 on 2025/4/11.
//

#include "character.h"
#include <sstream>
#include <iostream>
#include "put_image.h"

Character::Character(const std::string& path,
                     const std::string& character_type,
                     int num,
                     int interval) :
                     interval_ms(interval),
                     frame_list_left(ResourceManager::GetFramesLeft(character_type)),
                     frame_list_right(ResourceManager::GetFramesRight(character_type)){

    ResourceManager::LoadResources(path, num, character_type);

}

void Character::Play(int x, int y, int delta) {
    auto& current_frames = facing_left ? frame_list_left : frame_list_right;

    timer += delta;
    if(timer > interval_ms){
        idx_frame += 1;
        idx_frame = idx_frame % current_frames.size();
        timer = 0;
    }
    put_image_alpha(x, y, current_frames[idx_frame].get());
}

void Character::SetFacingLeft(int dir_x){
    if(dir_x>0) facing_left = false;
    if(dir_x<0) facing_left = true;
}

bool Character::facing_left = false;
